package com.solo.eggsolo.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.solo.eggsolo.aop.Logging;
import com.solo.eggsolo.common.bean.JsonResult;
import com.solo.eggsolo.common.bean.Pager;
import com.solo.eggsolo.entity.Admin;
import com.solo.eggsolo.service.AdminService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @RestController控制器默认返回Body
 * @author acer
 *
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员")
public class AdminController {
	@Autowired
	private AdminService adminService;
	
	@ApiOperation("所有管理员的信息")
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	public Pager<Admin> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer size) {

		Pager<Admin> pager = new Pager<>(page, size);

		List<Admin> list = adminService.list(pager);
		int total = adminService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);

		return pager;
	}

	@ApiOperation("通过管理员id查询管理员信息")
	@RequestMapping(value = "/findById", method = { RequestMethod.GET })
	@Logging
	public JsonResult findById(Integer id) {

		JsonResult result = new JsonResult(500, Boolean.FALSE, "查询失败");
		Admin admin = adminService.findById(id);

		if (admin != null) {
			result = new JsonResult(200, Boolean.TRUE, "查询成功", admin);
		}

		return result;
	}
	
	@ApiOperation("通过token查询管理员信息")
	@RequestMapping(value="/adminInfo", method = {RequestMethod.GET})
	public JsonResult adminInfo(String token) {
		JsonResult result = new JsonResult(500,Boolean.FALSE,"查询失败");
		Admin admin = adminService.adminInfo(token);
		
		if(admin != null) {
			result = new JsonResult(200,Boolean.TRUE,"查询成功",admin);
		}
		return result;
		
	}

	@ApiOperation("添加管理员")
	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	public JsonResult add(Admin admin) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "新增失败");

		//		用户注册时，取服务器的系统时间
		admin.setCreateTime(new Date());

		boolean ret = adminService.add(admin);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "新增成功");
		}
		return result;
	}

	@ApiOperation("修改管理员信息，id必填")
	@RequestMapping(value = "/edit", method = { RequestMethod.POST })
	public JsonResult edit(Admin admin) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "修改失败");
		boolean ret = adminService.edit(admin);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "修改成功");
		}
		return result;
	}
	
	@ApiOperation("重置管理员密码")
	@RequestMapping(value = "/resetPassword", method = { RequestMethod.POST })
	public JsonResult resetPassword(Integer id) {
		
		JsonResult result = new JsonResult(500, Boolean.FALSE,"重置失败");
		
		boolean ret = adminService.resetPassword(id);
		if(ret) {
			result =  new JsonResult(200, Boolean.TRUE,"重置成功");
		}
				return result;
	}

	@ApiOperation("删除管理员，id必填")
	@RequestMapping(value = "delete", method = { RequestMethod.POST })
	public JsonResult delete(Admin admin) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "删除失败");

		List<Admin> list = new ArrayList<Admin>();
		list.add(admin);

		boolean ret = adminService.deleteByIds(list);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "删除成功");
		}

		return result;
	}

}